package thread;

import lombok.extern.slf4j.Slf4j;

import java.util.ArrayDeque;
import java.util.Deque;
import java.util.HashSet;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.ReentrantLock;

/**
 * 手写一个线程池
 *
 * @author ganbo
 * @date 2022/10/28 15:21
 */

@Slf4j
public class MyThreadPool {

    //任务队列
    private BlockingQueue<Runnable> taskQueue;

    //线程集合
    private HashSet<Worker> workers = new HashSet();

    //核心线程数
    private int coreSize;

    //获取任务的超时时间
    private long timeout;

    //超时时间单位
    private TimeUnit timeUnit;

    class Worker {

    }

    public MyThreadPool(BlockingQueue<Runnable> taskQueue, HashSet<Worker> workers, int coreSize, long timeout, TimeUnit timeUnit, int queueCapcity) {
        this.taskQueue = new BlockingQueue<>(queueCapcity);
        this.workers = workers;
        this.coreSize = coreSize;
        this.timeout = timeout;
        this.timeUnit = timeUnit;
    }
}

class BlockingQueue<T> {

    //1:任务队列（这里采用双向链表）
    private Deque<T> queue = new ArrayDeque<>();


    //2：锁
    private ReentrantLock lock = new ReentrantLock();

    //3：生产者条件变量
    private Condition fullWaiSet = lock.newCondition();

    //4：消费者条件变量
    private Condition emptyWaitSet = lock.newCondition();

    //5：容量
    private int capcity;

    public BlockingQueue(int capcity) {
        this.capcity = capcity;
    }

    //带超时的阻塞获取
    public T poll(long timeout, TimeUnit unit) {
        lock.lock();
        try {
            //将timeout统一转化为纳秒
            long nanos = unit.toNanos(timeout);

            while (queue.isEmpty()) {
                try {
                    //返回的剩余时间
                    if (nanos <= 0) {
                        return null;
                    }
                    nanos = emptyWaitSet.awaitNanos(nanos);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }

            T first = queue.getFirst();
            fullWaiSet.signal();
            return first;
        } finally {
            lock.unlock();
        }
    }

    //阻塞获取
    public T take() {
        lock.lock();
        try {
            while (queue.isEmpty()) {
                try {
                    emptyWaitSet.await();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }

            T first = queue.getFirst();
            fullWaiSet.signal();
            return first;
        } finally {
            lock.unlock();
        }
    }

    //阻塞添加
    public void put(T element) {
        lock.lock();
        try {
            while (queue.size() == capcity) {
                try {
                    fullWaiSet.await();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }

            queue.addLast(element);
            emptyWaitSet.signal();
        } finally {
            lock.unlock();
        }
    }

    //获取大小
    public int size() {
        lock.lock();
        try {
            return queue.size();
        } finally {
            lock.unlock();
        }
    }


}




